# Maintainer: Jordan Irwin <antumdeluge@gmail.com>

_realname=md4c
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=0.4.8
pkgrel=2
pkgdesc="C Markdown parser implementation compliant to CommonMark specification"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/mity/md4c"
license=("MIT")
depends=()
makedepends=("bsdtar"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(${_realname}-${pkgver}.tar.gz::"${url}/archive/release-${pkgver}.tar.gz")
sha256sums=('4a457df853425b6bb6e3457aa1d1a13bccec587a04c38c622b1013a0da41439f')

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
      extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
      extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  dir_shared="${srcdir}/build-${CARCH}-shared"
  [[ -d "${dir_shared}" ]] && rm -rf "${dir_shared}"
  mkdir -p "${dir_shared}" && cd "${dir_shared}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake" \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=ON \
      "${extra_config[@]}" \
      "../${_realname}-release-${pkgver}"

  ninja

  dir_static="${srcdir}/build-${CARCH}-static"
  [[ -d "${dir_static}" ]] && rm -rf "${dir_static}"
  mkdir -p "${dir_static}" && cd "${dir_static}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake" \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=OFF \
      "${extra_config[@]}" \
      "../${_realname}-release-${pkgver}"

  ninja
}

package() {
  dir_shared="${srcdir}/build-${CARCH}-shared"
  dir_static="${srcdir}/build-${CARCH}-static"

  cd "${dir_shared}"
  DESTDIR="${pkgdir}" ninja install

  # static libs
  install -vDm644 "${dir_static}/src/libmd4c.a" "${pkgdir}${MINGW_PREFIX}/lib/libmd4c.a"
  install -vDm644 "${dir_static}/src/libmd4c-html.a" "${pkgdir}${MINGW_PREFIX}/lib/libmd4c-html.a"

  # license
  install -vDm644 "${srcdir}/${_realname}-release-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
